package com.mk.admin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期格式化
 */
public class DateTool {
    private DateTool() {
    }

    private static final String REGEX_ALL = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyy-MM-dd hh:mm:ss
     *
     * @param regex
     * @return
     */
    public static String nullToNowDate(String regex) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(regex);
        return simpleDateFormat.format(new Date());
    }

    public static String format(String regex) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(regex);
        return simpleDateFormat.format(new Date());
    }

    public static String format() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 得到当前时间减去或者加上second秒
     *
     * @param second
     * @return
     */
    public static String format(long second) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(new Date(date.getTime() + second * 1000));
    }

    public static String format(Date date, String regex) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(regex);
        return simpleDateFormat.format(date);
    }

    public static long getSecond(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = simpleDateFormat.parse(date);
            return parse.getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getSecondBetween(String startDate, String endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse1 = simpleDateFormat.parse(startDate);
            Date parse2 = simpleDateFormat.parse(endDate);
            return (parse2.getTime() - parse1.getTime()) / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getSecondBetween(String startDate, Date endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse1 = simpleDateFormat.parse(startDate);
            return (endDate.getTime() - parse1.getTime()) / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getSecondBetween(Date startDate, String endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse2 = simpleDateFormat.parse(endDate);
            return (parse2.getTime() - startDate.getTime()) / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getSecondBetween(Date startDate, Date endDate) {
        return (startDate.getTime() - endDate.getTime()) / 1000;
    }

    /**
     * 获得当日0点
     *
     * @since
     */
    public static String getDayZero() {
        return format(new Date(), "yyyy-MM-dd 00:00:00");
    }

    /**
     * 得到某天日期的开始时间
     *
     * @param day day为正标识几天后的day为负数标识几天前
     * @return
     */
    public static String getDayZero(int day) {
        LocalDate localDate = LocalDate.now().minusDays(day);
        LocalDateTime startOfDay = localDate.atStartOfDay();
// 格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return startOfDay.format(formatter);
    }

    /**
     * 获得当日最后时间
     *
     * @since
     */
    public static String getDayLast() {
        return format(new Date(), "yyyy-MM-dd 23:59:59");
    }

    /**
     * 得到某天的结束时间
     *
     * @return
     */
    public static String getDayLast(int day) {
        LocalDate localDate = LocalDate.now().minusDays(day);
        LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
// 格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return endOfDay.format(formatter);
    }
}
